﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuanLyDeTaiKHCN.Obj_class;
namespace QuanLyDeTaiKHCN.UserControls
{
    public partial class ThanhVien : System.Web.UI.UserControl
    {
        _dbDataContext _db = new _dbDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CatalogAccess.BindDatatoDropList(1, drlLinhVuc);
                CatalogAccess.BindDatatoDropList(2, drlHocHamHocVi);
                CatalogAccess.BindDatatoDropList(3, drlChucVu);
                BindGrid();
            }
        }
        protected void BindGrid()
        {
            _dbDataContext _db = new _dbDataContext();
            var list = from p in _db.tblHoiDongKhoaHocs select new 
            {
                ID=p.ID,HoTen=p.HoTen.Trim(),
                HocHamHocVi=p.tblDanhMuc.TenDanhMuc.Trim(),
                ChucVu=p.tblDanhMuc1.TenDanhMuc.Trim(),
                LinhVuc=p.tblDanhMuc2.TenDanhMuc.Trim(),
                DienThoai = p.DienThoai.Trim()
            };
            grid.DataSource = list.ToList();
            grid.DataBind();
        }

        protected void btnsave_Click(object sender, EventArgs e)
        {
            if (txtid.Value == "")//truong  hop them moi
            {
                if (CatalogAccess.InsertMember(txtHoTen.Text,nam.Checked, int.Parse(drlHocHamHocVi.SelectedValue.ToString()), int.Parse(drlChucVu.SelectedValue.ToString()), int.Parse(drlLinhVuc.SelectedValue.ToString()), txtNoiCongTac.Text, txtDiaChi.Text, txtDienThoai.Text, txtEmail.Text))
                {
                    lblstatus.Text = "Thêm thành công.";
                    BindGrid();
                    ClearTextBox();
                }
                else
                {
                    lblstatus.Text = "Không lưu được, kiểm tra lại.";
                }
            }
            else//truong hop cap nhat
            {
                if (CatalogAccess.UpdateMember(int.Parse(txtid.Value), txtHoTen.Text,nam.Checked, int.Parse(drlHocHamHocVi.SelectedValue.ToString()), int.Parse(drlChucVu.SelectedValue.ToString()), int.Parse(drlLinhVuc.SelectedValue.ToString()), txtNoiCongTac.Text, txtDiaChi.Text, txtDienThoai.Text, txtEmail.Text))
                {
                    lblstatus.Text = "Cập nhật thành công.";
                    BindGrid();
                }
                else
                {
                    lblstatus.Text = "Không lưu được, xin kiểm tra lại.";
                }
            }
        }
        protected void ClearTextBox()
        {
            txtHoTen.Text = "";
            drlHocHamHocVi.SelectedValue = "-1";
            drlChucVu.SelectedValue = "-1";
            drlLinhVuc.SelectedValue = "-1";
            txtNoiCongTac.Text = "";
            txtDiaChi.Text = "";
            txtDienThoai.Text = "";
            txtEmail.Text = "";
            txtid.Value = "";
        }

        protected void grid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DeleteMember")
            {
                if (CatalogAccess.DeleteMember(int.Parse(e.CommandArgument.ToString())))
                {
                    BindGrid();
                }
            }
            else if (e.CommandName == "SelectMember")
            {
                var item = _db.tblHoiDongKhoaHocs.Single(r => r.ID == int.Parse(e.CommandArgument.ToString()));
                txtid.Value = item.ID.ToString();
                txtHoTen.Text = item.HoTen.Trim();
                if ((bool)item.Phai == true)
                {
                    nam.Checked = true;
                }
                else
                {
                    nu.Checked = true;
                }
                drlHocHamHocVi.SelectedValue = item.HocHamHocVi.ToString();
                drlChucVu.SelectedValue = item.ChucVu.ToString();
                drlLinhVuc.SelectedValue = item.LinhVuc.ToString();
                txtNoiCongTac.Text = item.NoiCongTac.Trim();
                txtDiaChi.Text = item.DiaChi.Trim();
                txtDienThoai.Text = item.DienThoai.Trim();
                txtEmail.Text = item.Email.Trim();
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            ClearTextBox();
        }
    }
}